package com.masterclient.app.modules.cart.holder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.modules.cart.adapter.ShopCartAdapterListener;
import com.masterclient.app.modules.cart.beans.ProductsItemBean;
import com.masterclient.app.modules.cart.beans.PromotionsBean;
import com.masterclient.app.modules.cart.beans.PropertiesBean;
import com.masterclient.app.utils.CommonUtil;
import com.masterclient.app.widget.PriceIntegralLayout;
import com.masterclient.app.widget.TagTextView;

import java.util.ArrayList;
import java.util.List;

public class GoodsItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    private CheckBox checkBox;
    private ImageView ivCanNotCheck;
    private ImageView ivGood;
    private ImageView ivActivity;
    private TagTextView tvTitle;
    private TextView tvSku;
    private TextView tvStockLess;
    private PriceIntegralLayout llPrice;
    private TextView tvGoodNum;
    private ImageView ivReduce;
    private ImageView ivPlus;
    private LinearLayout llCartPresentContainer;
    private LinearLayout viewNoPresent;
    private TextView tvDesc;
    private View viewDivide;
    private View viewTopLine;
    private View viewBottomLine;
    private View viewMjLine;
    private View viewMjHalfLine;

    private Context mContext;
    private ShopCartAdapterListener mListener;
    private ProductsItemBean productsItemBean;
    private int position;

    public GoodsItemHolder(ViewGroup itemView, ShopCartAdapterListener listener) {
        super(LayoutInflater.from(itemView.getContext()).inflate(R.layout.view_item_cart_good, itemView, false));
        mContext = itemView.getContext();
        mListener = listener;
        LinearLayout viewItem = this.itemView.findViewById(R.id.view_item);
        RelativeLayout rlCheck = this.itemView.findViewById(R.id.rl_check);
        checkBox = this.itemView.findViewById(R.id.checkBox);
        ivCanNotCheck = this.itemView.findViewById(R.id.iv_cannot_check);
        ivGood = this.itemView.findViewById(R.id.iv_good);
        ivActivity = this.itemView.findViewById(R.id.iv_activity);
        tvTitle = this.itemView.findViewById(R.id.tv_title);
        tvSku = this.itemView.findViewById(R.id.tv_skus);
        tvStockLess = this.itemView.findViewById(R.id.tv_stock_less);
        llPrice = this.itemView.findViewById(R.id.ll_price);
//        tvIntergalPrice = this.itemView.findViewById(R.id.tv_integral_price);
//        intergalPriceFlag = this.itemView.findViewById(R.id.integral_price_flag);
//        tvMoneyFlag = this.itemView.findViewById(R.id.money_flag);
//        tvJiFen = this.itemView.findViewById(R.id.tv_jifen);
        tvGoodNum = this.itemView.findViewById(R.id.tv_good_num);
        ivReduce = this.itemView.findViewById(R.id.iv_reduce);
        ivPlus = this.itemView.findViewById(R.id.iv_plus);
        llCartPresentContainer = this.itemView.findViewById(R.id.ll_cart_present_container);
        viewNoPresent = this.itemView.findViewById(R.id.view_no_present);
        tvDesc = this.itemView.findViewById(R.id.tv_desc);
        TextView tvCollect = this.itemView.findViewById(R.id.tv_collect);
        TextView tvDelete = this.itemView.findViewById(R.id.tv_delete);
        viewDivide = this.itemView.findViewById(R.id.v_divide);
        viewTopLine = this.itemView.findViewById(R.id.view_top_line);
        viewBottomLine = this.itemView.findViewById(R.id.view_bottom_line);
        viewMjLine = this.itemView.findViewById(R.id.view_mj_line);
        viewMjHalfLine = this.itemView.findViewById(R.id.view_mj_half_line);

//        Utils.setDin(tvPrice,mContext);
//        Utils.setDin(tvIntergalPrice,mContext);
//        Utils.setDin(tvMoneyFlag,mContext);
//        Utils.setDin(intergalPriceFlag,mContext);
        viewItem.setOnClickListener(this);
        ivReduce.setOnClickListener(this);
        ivPlus.setOnClickListener(this);
        tvCollect.setOnClickListener(this);
        tvDelete.setOnClickListener(this);
        rlCheck.setOnClickListener(this);
        checkBox.setOnClickListener(this);
    }

    public void onBindViewHolder(ProductsItemBean itemBean, int position) {
        productsItemBean = itemBean;
        this.position = position;

        //商品是否可勾选
        if (itemBean.isUsable) {
            checkBox.setVisibility(View.VISIBLE);
            ivCanNotCheck.setVisibility(View.GONE);
        } else {
            checkBox.setVisibility(View.GONE);
            ivCanNotCheck.setVisibility(View.VISIBLE);
        }
        //商品是否被选中
        if (itemBean.isCheck) {
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }

        //店内商品分割线
        if (itemBean.showLine) {
            viewDivide.setVisibility(View.VISIBLE);
        } else {
            viewDivide.setVisibility(View.GONE);
        }

        //满减活动
        if (itemBean.isShowHalfLine && itemBean.isCheck){
            viewTopLine.setVisibility(View.GONE);
            viewMjHalfLine.setVisibility(View.GONE);
            viewBottomLine.setVisibility(View.GONE);
            viewMjLine.setVisibility(View.GONE);
        }else {
            viewTopLine.setVisibility(View.GONE);
            viewMjHalfLine.setVisibility(View.GONE);
            viewBottomLine.setVisibility(View.GONE);
            viewMjLine.setVisibility(View.GONE);
        }

        if (itemBean.isShowAllLine && itemBean.isCheck){
            viewTopLine.setVisibility(View.GONE);
            viewMjHalfLine.setVisibility(View.GONE);
            viewBottomLine.setVisibility(View.GONE);
            viewMjLine.setVisibility(View.GONE);
        }else {
            viewTopLine.setVisibility(View.GONE);
            viewMjHalfLine.setVisibility(View.GONE);
            viewBottomLine.setVisibility(View.GONE);
            viewMjLine.setVisibility(View.GONE);
        }

        if (!itemBean.isShowHalfLine && !itemBean.isShowAllLine){
            viewTopLine.setVisibility(View.GONE);
            viewMjHalfLine.setVisibility(View.GONE);
            viewBottomLine.setVisibility(View.GONE);
            viewMjLine.setVisibility(View.GONE);
        }

        ViewTransformUtil.glideImageView(mContext, itemBean.image, ivGood,
                R.mipmap.defaults, ScreenUtils.dp2px(107), ScreenUtils.dp2px(107));

        //图片上活动图标
        double ratio = 1;
        if (!TextUtils.isEmpty(itemBean.image_width)){
            ratio = ScreenUtils.dp2px(107)/Double.parseDouble(itemBean.image_width);
        }
        if(itemBean.iconInfo!=null
                && !TextUtils.isEmpty(itemBean.iconInfo.iconHeight)
                &&!TextUtils.isEmpty(itemBean.iconInfo.iconWidth)) {
            int leftMargin = (int) (Integer.parseInt(itemBean.iconInfo.iconLeftMargin)*ratio);
            int topMargin = (int) (Integer.parseInt(itemBean.iconInfo.iconTopMargin)*ratio);
            int bottomMargin = (int) (Integer.parseInt(itemBean.iconInfo.iconBottomMargin)*ratio);
            ViewTransformUtil.glideImageView(mContext,itemBean.iconInfo.iconImage,ivActivity,R.mipmap.defaults);
            ivActivity.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivActivity.getLayoutParams();
            params.width = (int) (Integer.valueOf(itemBean.iconInfo.iconWidth)*ratio);
            params.height = (int) (Integer.valueOf(itemBean.iconInfo.iconHeight)*ratio);
            params.addRule(RelativeLayout.ALIGN_BOTTOM,0);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);
            params.setMargins(0, 0, 0, 0);
            switch (itemBean.iconInfo.iconMarginType) {
                case "1":
                    params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.iv_good);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(leftMargin, topMargin, 0, bottomMargin);
                    break;
                case "2":
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(leftMargin, topMargin, 0, bottomMargin);
                    break;
                default:break;
            }
            ivActivity.setLayoutParams(params);
        }else {
            ivActivity.setVisibility(View.GONE);
        }

        //新人专享Tag
            List<String> tagList = new ArrayList<>();
            if (!TextUtils.isEmpty(itemBean.labelName)) {
                tagList.add(itemBean.labelName);
            }

        boolean isVipExclusive = !TextUtils.isEmpty(itemBean.integralProductVipExclusive)&&"1".equals(itemBean.integralProductVipExclusive);

        tvTitle.setContentAndTag((tagList.size()>0&&isVipExclusive?" ":"")  + itemBean.title, tagList,isVipExclusive);
//        } else {
//            tvTitle.setText(itemBean.title);
//        }
        tvTitle.setMaxLines(2);

        //sku
        List<PropertiesBean> listProperty = itemBean.properties;
        StringBuilder sku = new StringBuilder();
        if (listProperty != null) {
            sku.append("规格：");
            for (PropertiesBean property : listProperty) {
                sku.append(property.aliasName).append("; ");
            }
        }
        tvSku.setText(sku);

        //限购数量和库存不足
        tvStockLess.setText(TextUtils.isEmpty(itemBean.stockTip) ? "" : itemBean.stockTip);
        llPrice.setData(itemBean.integralPrice, itemBean.price);
        //普通价格、金币价格、新人价格
//        if ("2".equals(itemBean.productType)&&!TextUtils.isEmpty(itemBean.integralPrice)&&Integer.parseInt(itemBean.integralPrice)>0) {
//            //金币价格
//            tvPrice.setText(itemBean.integralPrice);
////            tvJiFen.setVisibility(View.VISIBLE);
//            tvMoneyFlag.setVisibility(View.GONE);
//            tvJiFen.setText(!TextUtils.isEmpty(itemBean.price)&&Float.parseFloat(itemBean.price)>0?" 金币+":"金币");
//            tvJiFen.setVisibility(!TextUtils.isEmpty(itemBean.integralPrice)&&Integer.parseInt(itemBean.integralPrice)>0?View.VISIBLE:View.GONE);
//            tvPrice.setVisibility(!TextUtils.isEmpty(itemBean.integralPrice)&&Integer.parseInt(itemBean.integralPrice)>0?View.VISIBLE:View.GONE);
//            tvIntergalPrice.setVisibility(!TextUtils.isEmpty(itemBean.price)&&Float.parseFloat(itemBean.price)>0?View.VISIBLE:View.GONE);
//            intergalPriceFlag.setVisibility(!TextUtils.isEmpty(itemBean.price)&&Float.parseFloat(itemBean.price)>0?View.VISIBLE:View.GONE);
//            tvIntergalPrice.setText(TextUtils.isEmpty(itemBean.price)?"":itemBean.price);
//        } else {
//            tvJiFen.setVisibility(View.GONE);
//            tvMoneyFlag.setVisibility(View.VISIBLE);
//            tvIntergalPrice.setVisibility(View.GONE);
//            intergalPriceFlag.setVisibility(View.GONE);
//            //普通价格、新人价格
//            tvPrice.setText(itemBean.price);
//        }

        if ("1".equals(itemBean.num)) {
            ivReduce.setImageResource(R.mipmap.reduce_unclick);
        } else {
            ivReduce.setImageResource(R.mipmap.reduce_click);
        }
        if (!TextUtils.isEmpty(itemBean.stock) && Integer.valueOf(itemBean.stock) <= Integer.valueOf(itemBean.num)) {
            ivPlus.setImageResource(R.mipmap.plus_unclick);
        } else {
            ivPlus.setImageResource(R.mipmap.plus_click);
        }
        tvGoodNum.setText(itemBean.num);

        //赠品列表
        if (itemBean.promotions != null && itemBean.promotions.promotionList != null &&
                itemBean.promotions.promotionList.size() > 0) {
            llCartPresentContainer.setVisibility(View.VISIBLE);
            viewNoPresent.setVisibility(View.GONE);
            llCartPresentContainer.removeAllViews();
            for (PromotionsBean.PromotionListBean promotionListBean : itemBean.promotions.promotionList) {
                View view_present_goods = View.inflate(mContext, R.layout.item_cart_present, null);
                TextView tvPresentTag = view_present_goods.findViewById(R.id.tv_present_tag);
                TextView ivPresentName = view_present_goods.findViewById(R.id.tv_present_name);
                TextView ivPresentNum = view_present_goods.findViewById(R.id.tv_present_num);
                view_present_goods.setOnClickListener(view1 -> {
                    if (promotionListBean.type != null) {
                        CommonUtil.goAnyWhere(mContext, promotionListBean.type, promotionListBean.targetId, "", "", "");
                    }
                });

                tvPresentTag.setText(promotionListBean.tag);
                ivPresentName.setText(promotionListBean.name);
                ivPresentNum.setText("x" + promotionListBean.num);
                llCartPresentContainer.addView(view_present_goods);
            }
        } else {
            llCartPresentContainer.setVisibility(View.GONE);
        }

        //赠送赠品条件
        if (itemBean.promotions != null && !TextUtils.isEmpty(itemBean.promotions.promotionDesc)) {
            viewNoPresent.setVisibility(View.VISIBLE);
            tvDesc.setText(itemBean.promotions.promotionDesc);
        } else {
            viewNoPresent.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.view_item:
                mListener.onGoIntoGoodDetails(productsItemBean.productMainId, position);
                break;
            case R.id.rl_check:
            case R.id.checkBox:
                checkBox.setChecked(!productsItemBean.isCheck);
                if (checkBox.isChecked()) {
                    mListener.onCheckGoods(productsItemBean.splitSuffix, productsItemBean.productId, true);
                } else {
                    mListener.onCheckGoods(productsItemBean.splitSuffix, productsItemBean.productId, false);
                }
                break;
            case R.id.iv_reduce:
                int buyQty = Integer.valueOf(productsItemBean.num);
                int reduceStock = Integer.valueOf(productsItemBean.stock);
                if (reduceStock < buyQty && reduceStock > 0) {
                    tvGoodNum.setText(String.valueOf(reduceStock));
                    ivReduce.setImageResource(R.mipmap.reduce_click);
                    mListener.onReduceGoods(productsItemBean.cartId, reduceStock + "");
                } else {
                    if (buyQty > 1) {
                        tvGoodNum.setText(String.valueOf(buyQty--));
                        ivReduce.setImageResource(R.mipmap.reduce_click);
                        mListener.onReduceGoods(productsItemBean.cartId, buyQty + "");
                    } else {
                        ivReduce.setImageResource(R.mipmap.reduce_unclick);
                        ToastUtil.showToast("请至少购买一件哦");
                    }
                }
                break;
            case R.id.iv_plus:
                int buyNum = Integer.valueOf(productsItemBean.num);
                int stock = Integer.valueOf(productsItemBean.stock);
                int limitNum = TextUtils.isEmpty(productsItemBean.limitNum) ? 0 : Integer.valueOf(productsItemBean.limitNum);
                //有限购数量
                if (!TextUtils.isEmpty(productsItemBean.limitNum) && limitNum > 0) {
                    //1、限购数量>=库存
                    if (limitNum >= stock) {
                        if (buyNum < stock) {
                            buyNum++;
                            tvGoodNum.setText(String.valueOf(buyNum));
                            mListener.onPlusGoods(productsItemBean.cartId, buyNum + "");
                        } else {//已经达到库存上线
                            ivPlus.setImageResource(R.mipmap.plus_unclick);
                            ToastUtil.showToast("已达到库存上限");
                        }
                    } else {
                        //2、限购数量<库存
                        if (buyNum < limitNum) {
                            buyNum++;
                            tvGoodNum.setText(String.valueOf(buyNum));
                            mListener.onPlusGoods(productsItemBean.cartId, buyNum + "");
                        } else {//已经达到限购上线
                            ivPlus.setImageResource(R.mipmap.plus_unclick);
                            ToastUtil.showToast("已达到限购上限");
                        }
                    }
                } else {
                    //无限购数量
                    if (buyNum < stock) {
                        tvGoodNum.setText(String.valueOf(buyNum++));
                        mListener.onPlusGoods(productsItemBean.cartId, buyNum + "");
                    } else {
                        ivPlus.setImageResource(R.mipmap.plus_unclick);
                        ToastUtil.showToast("没有更多库存了");
                    }
                }
                break;
            case R.id.tv_collect:
                mListener.onCollectGoods(productsItemBean.cartId);
                break;
            case R.id.tv_delete:
                mListener.onDeleteGoods(productsItemBean.cartId);
                break;
        }
    }


}
